import pymysql

from setting.mysql_setting import *

class Connect_Mysql:

    def __init__(self):

        self.conn = pymysql.connect(
            host = HOST,
            port = PORT,
            user = USER,
            password = PASSWORD,
            db = DB,
            charset = CHARSET,
        )

        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        self.LID_SQL = 'SELECT LAST_INSERT_ID()'

    def close(self):

        self.cursor.close()

        self.conn.close()

    def __enter__(self):

        return self

    def __exit__(self, *exc_info):

        if exc_info:

            self.conn.rollback()

        self.close()

    def select_info(self,sql,args = None):

        if args == None:

            self.cursor.execute(sql)

        else:

            self.cursor.execute(sql,args)

        Info = self.cursor.fetchone()

        return Info


    def insert_company(self,sql,args):

        if args == None:

            self.cursor.execute(sql)

        else:

            self.cursor.execute(sql,args)

        ID = self.select_info(self.LID_SQL)

        self.conn.commit()

        return ID

    def update_openid(self,openid,company_id=None):

        if company_id:

            sql = 'update openid set company_id=%s where openid=%s'

            self.cursor.execute(sql,(company_id,openid))

            self.conn.commit()

            return '插入成功1'
            
        else:

            sql = 'insert into openid(openid)value(%s)'

            self.cursor.execute(sql,openid)

            self.conn.commit()

            return '插入成功2'

            

#     pay == '1'

#     sql = 'insert into openid(openid,company_id,pay)values(%s,%s,%s)'

#     self.cursor.execute(sql,(info,args,pay))

#     self.conn.commit()


if __name__ == "__main__":

    sql = 'insert into company_copy1(company_name,linkman,phone,need,party)values(%s,%s,%s,%s,%s)'
    
    # print(Connect_Mysql().insert_company(sql,("3",'2222','12312312312','222','3')))

    # Connect_Mysql().insert_openid('3','1')

    # pass



# import uuid

# print(uuid.uuid1().int)
# phen = [False]
# print(all(phen))